package zhoug.common.zxing.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import zhoug.common.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 扫一扫搜索页面
 *
 * @Author 35574
 * @Date 2022/4/19
 * @Description
 */
public class SearchActivity extends AppCompatActivity {
    public static final String PARAM_RESULT = "resultData";
    private ImageView mIvBack;
    private EditText mEditText;
    private ImageView mIvSearch;
    public static final String PARAM_SEARCH_HINT_TEXT = "searchHintText";//搜索提示
    private static final String DEFAULT_HINT = "请输入二维码/条形码";
    private String mSearchHintText = DEFAULT_HINT;//搜索提示

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_zxing_search_activity);
        setStatusBarColor();
        initIntentData();
        findViews();
        init();
    }

    private void initIntentData() {
        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            try {
                mSearchHintText = extras.getString(PARAM_SEARCH_HINT_TEXT, DEFAULT_HINT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void setStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(0xffffffff);
        }
        View decor = getWindow().getDecorView();
//        if (isDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
//        } else {
//            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//        }
    }

    private void findViews() {
        mIvBack = findViewById(R.id.iv_back);
        mEditText = findViewById(R.id.editText);
        mIvSearch = findViewById(R.id.iv_search);
    }

    private void init() {
        mEditText.setHint(mSearchHintText);
        mIvBack.setOnClickListener(v -> finish());
        mIvSearch.setOnClickListener(v -> confirm());
        mEditText.setOnEditorActionListener((v, actionId, event) -> {
            int searchId = getResources().getInteger(R.integer.zxing_ime_search_id);
            if (actionId == searchId || actionId == EditorInfo.IME_ACTION_SEARCH) {
                confirm();
                return true;
            }
            return false;
        });

    }


    private void confirm() {
        Editable text = mEditText.getText();
        String searchName = text != null ? text.toString() : null;
        if (TextUtils.isEmpty(searchName)) {
            Toast.makeText(this, mSearchHintText, Toast.LENGTH_SHORT).show();
        } else {
            Intent result = new Intent();
            result.putExtra(PARAM_RESULT, searchName);
            setResult(RESULT_OK, result);
            finish();
        }

    }

}
